package com.itlong.cloud.sdk.service.impl;

import com.itlong.cloud.POJO.DTO.sdk.CloudElevatorSDKServerElevatorKeyDTO;
import com.itlong.cloud.POJO.PO.PropertyRoleProjectPO;
import com.itlong.cloud.app.dao.IAppCloudElevatorCallStatusDao;
import com.itlong.cloud.enumerate.MultiElevatorAuthEnum;
import com.itlong.cloud.sdk.dao.ICloudElevatorSDKRobotDao;
import com.itlong.cloud.sdk.service.ICloudElevatorSDKRobotService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 * 云对讲SDK多媒体资源服务接口实现类
 * <desc/>
 *
 * @createDate 2018/05/22
 */
@Service
public class CloudElevatorSDKRobotServiceImpl implements ICloudElevatorSDKRobotService {

    @Autowired
    private ICloudElevatorSDKRobotDao iCloudElevatorSDKRobotDao;

    @Autowired
    IAppCloudElevatorCallStatusDao iAppCloudElevatorCallStatusDao;

    private static final int DEVICE_AUTH_STATUS_YES = 1;

    public static final int DEVICE_AUTH_STATUS_NO = 0;

    /**
     * <desc>
     *      查询云电梯唯一标识。
     * <desc/>
     *
     * @param pageDTO 入参
     * @return
     * @author Qiang.S
     * @createDate 2018-12-28
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<String> getElevatorKey(CloudElevatorSDKServerElevatorKeyDTO pageDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        List<String> list = null;
        if ("4".equals(pageDTO.getRoleId())) {//独立开发者
            PropertyRoleProjectPO propertyRoleProjectPO = iCloudElevatorSDKRobotDao.getOriginalProjectInfoByRole4(params);
            if (propertyRoleProjectPO != null) {
                //当开启了机器人乘梯api时
                if (1==propertyRoleProjectPO.getRobotElevatorApi()){
                    // 从机器人乘梯API授权设备信息中解析出deviceId
                    String deviceAuthStr = propertyRoleProjectPO.getDeviceAuth();
                if (StringUtils.isNotBlank(deviceAuthStr)) {
                    list = new ArrayList<>();
                    int i = deviceAuthStr.indexOf("1");
                    while (i != -1) {
                        int deviceInt = i + 1;
                        String deviceId = StringHandlerUtil.formatStrLen(deviceInt + "", "0", 4, false, false);
                        list.add(pageDTO.getProjectId() + "1" + deviceId);
                        i = deviceAuthStr.indexOf("1", deviceInt);
                    }
                }
             }
            }
            //机器人开发者及工程人员
        } else if ("7".equals(pageDTO.getRoleId()) || "1".equals(pageDTO.getRoleId())) {

            if (DEVICE_AUTH_STATUS_YES == pageDTO.getRobotDeviceAuthStatus()){
                list = iCloudElevatorSDKRobotDao.getRobotDevelopElevatorKey(params);
            }else{
                //兼容sdkV1接口
                String deviceType = pageDTO.getDeviceType() == null ? null : pageDTO.getDeviceType().toString();
                switch (MultiElevatorAuthEnum.getByType(deviceType)){
                    case MULTI_ELEVATOR_AUTH:
                        //多梯时找出项目下带群控器的设备
                        list = iCloudElevatorSDKRobotDao.getElevatorList(params);
                        break;
                    case SINGLE_ELEVATOR_AUTH:
                        //单梯时找出项目下所有的电梯设备
                        params.put("deviceType", null);
                        list = iCloudElevatorSDKRobotDao.getElevatorList(params);
                        break;
                        default:
                            list = new ArrayList<>();
                }
            }

        }
        return list;
    }

    /**
     * <desc>
     *      查询当前机器人行程是否完成。
     * <desc/>
     *
     * @param  robotId 机器人编号
     * @param  deviceUnique 设备编号
     * @return
     * @author Qiang.S
     * @createDate 2020-03-27
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer checkSuccessState(String robotId, String deviceUnique) throws Exception {
        return iAppCloudElevatorCallStatusDao.checkRecordState(robotId, deviceUnique);
    }

    /**
     * <desc>
     *      获取机器人单梯多梯权限标记
     * </desc>
     *
     * @param robotId 机器人id
     * @return 单梯多梯标记
     * @Author Zhu.yj
     * @createDate  2020/9/7
     */
    @Override
    public Map<String, Object> getElevatorFlag(String robotId) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        //查找该机器人拥有的电梯权限
        Map<String, Object> params = new HashMap<>();
        params.put("robotId", robotId);
        //机器人拥有多梯权限的电梯数量
        int multiElevatorCount = iCloudElevatorSDKRobotDao.getRobotMultiElevator(params);
        //机器人拥有单梯权限的电梯数量
        int singleElevatorCount = iCloudElevatorSDKRobotDao.getRobotSingleElevator(params);
        StringBuilder authFlag = new StringBuilder();
        if (multiElevatorCount > 0){
            if (singleElevatorCount >0){
                //1,2为同时拥有单梯和多梯的权限
                resultMap.put("flag", String.format("%s,%s", MultiElevatorAuthEnum.SINGLE_ELEVATOR_AUTH.getType(),MultiElevatorAuthEnum.MULTI_ELEVATOR_AUTH.getType()));
            }else {
                resultMap.put("flag", MultiElevatorAuthEnum.SINGLE_ELEVATOR_AUTH.getType());
            }
        }else {
            if (singleElevatorCount > 0){
                resultMap.put("flag", MultiElevatorAuthEnum.MULTI_ELEVATOR_AUTH.getType());
            }else {
                resultMap.put("flag", MultiElevatorAuthEnum.NONE_ELEVATOR_AUTH.getType());
            }
        }
        return resultMap;
    }
}
